{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 09 ``Numpy`` 中arg运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = np.linspace(-10, 10, 21)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0"
     },
     "metadata": {},
     "execution_count": 136
    }
   ],
   "source": [
    "# 求的是最小值的索引值\n",
    "np.argmin(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "-10.0"
     },
     "metadata": {},
     "execution_count": 137
    }
   ],
   "source": [
    "x[np.argmin(x)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "-10.0"
     },
     "metadata": {},
     "execution_count": 138
    }
   ],
   "source": [
    "np.min(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "20"
     },
     "metadata": {},
     "execution_count": 139
    }
   ],
   "source": [
    "np.argmax(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "10.0"
     },
     "metadata": {},
     "execution_count": 140
    }
   ],
   "source": [
    "x[np.argmax(x)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "10.0"
     },
     "metadata": {},
     "execution_count": 141
    }
   ],
   "source": [
    "np.max(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 排序和使用索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([-10.,  -9.,  -8.,  -7.,  -6.,  -5.,  -4.,  -3.,  -2.,  -1.,   0.,\n         1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])"
     },
     "metadata": {},
     "execution_count": 142
    }
   ],
   "source": [
    "x = np.linspace(-10, 10, 21)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ -9.,   0.,  10.,   9., -10.,  -2.,   6.,   3.,  -1.,  -4.,  -3.,\n         4.,  -7.,  -6.,   1.,  -8.,   7.,   5.,  -5.,   2.,   8.])"
     },
     "metadata": {},
     "execution_count": 143
    }
   ],
   "source": [
    "# 乱序处理\n",
    "np.random.shuffle(x)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([-10.,  -9.,  -8.,  -7.,  -6.,  -5.,  -4.,  -3.,  -2.,  -1.,   0.,\n         1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])"
     },
     "metadata": {},
     "execution_count": 144
    }
   ],
   "source": [
    "# 只是返回排好序的数列，本身没有排序\n",
    "np.sort(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ -9.,   0.,  10.,   9., -10.,  -2.,   6.,   3.,  -1.,  -4.,  -3.,\n         4.,  -7.,  -6.,   1.,  -8.,   7.,   5.,  -5.,   2.,   8.])"
     },
     "metadata": {},
     "execution_count": 145
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 这个是数组原地排序\n",
    "x.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([-10.,  -9.,  -8.,  -7.,  -6.,  -5.,  -4.,  -3.,  -2.,  -1.,   0.,\n         1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])"
     },
     "metadata": {},
     "execution_count": 147
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[9, 3, 4, 0],\n       [0, 2, 4, 7],\n       [2, 4, 3, 4],\n       [5, 0, 8, 1]])"
     },
     "metadata": {},
     "execution_count": 148
    }
   ],
   "source": [
    "X = np.random.randint(10, size=(4,4))\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 0, 3, 0],\n       [2, 2, 4, 1],\n       [5, 3, 4, 4],\n       [9, 4, 8, 7]])"
     },
     "metadata": {},
     "execution_count": 149
    }
   ],
   "source": [
    "# axis=0沿着行进行排序，也就是每一列都是有序的\n",
    "np.sort(X, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 3, 4, 9],\n       [0, 2, 4, 7],\n       [2, 3, 4, 4],\n       [0, 1, 5, 8]])"
     },
     "metadata": {},
     "execution_count": 150
    }
   ],
   "source": [
    "# axis=1沿着列进行排序，也就是每一行都是有序的\n",
    "np.sort(X, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([-10.,  -9.,  -8.,  -7.,  -6.,  -5.,  -4.,  -3.,  -2.,  -1.,   0.,\n         1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])"
     },
     "metadata": {},
     "execution_count": 151
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.shuffle(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([  9.,  -4.,   0.,  -7.,   6.,  -1.,   8.,   5.,   1.,  -6.,   4.,\n         7.,   2.,  -2.,  10.,  -5.,  -3., -10.,  -9.,   3.,  -8.])"
     },
     "metadata": {},
     "execution_count": 153
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "[17 18 20  3  9 15  1 16 13  5  2  8 12 19 10  7  4 11  6  0 14]\nTrue\n"
    }
   ],
   "source": [
    "# 返回的是排好序的数组的元素的索引。第一个索引就是最小的元素\n",
    "print(np.argsort(x))\n",
    "print(x[np.argsort(x)[0]] == np.min(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([ -9., -10.,  -8.,  -7.,  -6.,  -5.,  -1.,   3.,   1.,   0.,  -4.,\n        -3.,   2.,  -2.,   4.,  10.,   7.,   5.,   8.,   6.,   9.])"
     },
     "metadata": {},
     "execution_count": 155
    }
   ],
   "source": [
    "# 快排中的partition，保证了索引为3的数字前面一定比索引为3的数字小，索引为3的数字后面的一定比索引为3的数字大\n",
    "np.partition(x, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([18, 17, 20,  3,  9, 15,  5, 19,  8,  2,  1, 16, 12, 13, 10, 14, 11,\n        7,  6,  4,  0], dtype=int64)"
     },
     "metadata": {},
     "execution_count": 156
    }
   ],
   "source": [
    "# 上面方法的索引方法\n",
    "np.argpartition(x, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[9, 3, 4, 0],\n       [0, 2, 4, 7],\n       [2, 4, 3, 4],\n       [5, 0, 8, 1]])"
     },
     "metadata": {},
     "execution_count": 157
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "[[3 1 2 0]\n [0 1 2 3]\n [0 2 1 3]\n [1 3 0 2]]\nTrue\nTrue\nTrue\nTrue\n"
    }
   ],
   "source": [
    "# 每一行的排序结果的索引值。\n",
    "print(np.argsort(X, axis=1))\n",
    "for i in range(X.shape[0]):\n",
    "    print(X[i, np.argsort(X, axis=1)[i, 0]] == np.min(X, axis=1)[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[3, 1, 2, 0],\n       [0, 1, 2, 3],\n       [0, 2, 1, 3],\n       [1, 3, 0, 2]], dtype=int64)"
     },
     "metadata": {},
     "execution_count": 159
    }
   ],
   "source": [
    "# 分位的求索引\n",
    "np.argpartition(X, 2, axis=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}